<?php
class MoneyAction extends Action{
//model("Xdata")->getConfig("to_money_status","scoreSetting","admin_Score");exit;
	private $to_money_status;
	private $to_rate;
	private $to_min;
	
	public function _initialize(){
		$this->to_money_status = model("Xdata")->getConfig("to_money_status","scoreSetting","admin_Score");
		$this->to_rate = model("Xdata")->getConfig("to_rate","scoreSetting","admin_Score");
		$this->to_min = model("Xdata")->getConfig("to_min","scoreSetting","admin_Score");
		
	}
	
	
	public function getCredit(){//【获取积分】
	$uid = dt($_REQUEST['uid']);
	$credit_user = M('Credit_user');
	$get_credit = $credit_user->where("uid = $uid")->field("uid,score")->find();
	if($get_credit){
			$get_credit['money'] = $get_credit['score']/$this->to_rate;
			$value = ["code" =>"success",'data'=>$get_credit];
			header("Content-Type: text/json");
			die(et($value));
	}else{
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(et($value));
	}
	}
	
	public function checkStatusLocal(){
		if($this->to_money_status){
			return true;
		}else{
			return false;
		}
	
	}
	
	public function checkStatus(){
		if(!$this->checkStatusLocal()){//服务已关闭
			$value = ["code" =>"disable"];
			header("Content-Type: text/json");
			die(et($value));
		}else{
			$value = ["code" =>"enable"];
			header("Content-Type: text/json");
			die(et($value));
		}
	}
	
	public function toMoney(){//【提现申请】
		if(!$this->checkStatusLocal()){//服务已关闭
			$value = ["code" =>"disable"];
			dump($value);exit;
			header("Content-Type: text/json");
			die(et($value));
		}
		$uid = dt($_REQUEST['uid']);
		$credit = intval(dt($_REQUEST['credit']));
		$code = dt($_REQUEST['code']);
		$fpassword = dt($_REQUEST['fpassword']);
		
		if(empty($uid) || empty($credit) || empty($fpassword) || empty($code)){
			
			$value = ["code" =>"empty"];
			header("Content-Type: text/json");
			die(et($value));
		
		}
		//判断短信验证码是否正确
		if ($code!=$_SESSION['code'])
		{
// 			$value = ["code" =>"codefalse"];
// 			header("Content-Type: text/json");
// 			die(et($value));
		}
		$user_card = M('User_card');
		$card = $user_card->where("uid = $uid")->find();
		if(!$card){//未检测到有银行卡
			$value = ["code" =>"cardfalse"];
			header("Content-Type: text/json");
			die(et($value));
		}
		$user = M('User');
		
		$get_user = $user->where("uid = $uid")->field("fpassword,login_salt")->find();
		//echo $get_user['login_salt'];exit;
		$fpassword = md5(md5($fpassword).$get_user['login_salt']);
		//echo $fpassword;exit;
		if ($fpassword != $get_user["fpassword"]){//判断服务密码是否正确
			$value = ["code" =>"fpasswordwrong"];
			header("Content-Type: text/json");
			die(et($value));
		}
		$credit_user = M('Credit_user');
		$get_credit = $credit_user->where("uid = $uid")->field("score")->find();
		if ($credit > $get_credit["score"] || $credit < $this->to_min*$this->to_rate){//判断积分是否合法
			$value = ["code" =>"scorewrong"];
			header("Content-Type: text/json");
			die(et($value));
		}
		//定义新申请记录
		$money = M('User_to_money');
		$money->uid = $uid;
		$money->credit_consume = $credit;
		$money->money = $credit/$this->to_rate;
		$money->apply_time = time();
		$money->card_num = $card['card_num'];
		$money->card_name = $card['card_name'];
		$money->name = $card['name'];
		//扣除积分
		$data['score'] = $get_credit["score"]-$credit;
		if($credit_user->where("uid = $uid")->data($data)->save()){
			if($money->add()){//申请成功
				$value = ["code" =>"success"];
				header("Content-Type: text/json");
				die(et($value));
			}else{//申请失败，恢复原积分
				$data['score'] = $get_credit["score"];
				$credit_user->where("uid = $uid")->data($data)->save();
				$value = ["code" =>"false"];
				header("Content-Type: text/json");
				die(et($value));
			}
		}else{//申请失败
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(et($value));
		}
		
	
	}

	public function moneyLog(){//【获取账单】
		$uid = dt($_REQUEST['uid']);
		$money = M('User_to_money');
		$pagecount=isset($_REQUEST['pagecount'])?dt($_REQUEST['pagecount']):"10";//每页显示数量
		$pagenum=$pagecount*((isset($_REQUEST['pagenum'])?dt($_REQUEST['pagenum']):"1")-1);//当前页数
		$data = $money->where("uid = $uid")->limit("$pagenum,$pagecount")->order("apply_time desc")->select();
		
		if($data){
			$value = ["code" =>"success",'data'=>$data];
			header("Content-Type: text/json");
			die(et($value));
		}else{
			if(is_array($data) || mysql_num_rows($data) < 1){
				$value = ["code" =>"success",'data'=>array()];
				header("Content-Type: text/json");
				die(et($value));
			
			}
			$value = ["code" =>"false"];
			header("Content-Type: text/json");
			die(et($value));
		}
	
	}



}